package com.taraslaba.polvisachecker.server;

import java.util.logging.Logger;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Transaction;

public class ListenersDatastore {
	private static final Logger logger = Logger.getLogger(ListenersDatastore.class.getName());
	private static final DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	
	private static final String DEVICE_TYPE = "Device";
	private static final String DEVICE_REG_ID_PROPERTY = "regId";
	
	public void register(String regId){
		logger.info("Registering: " + regId);
		
		Transaction tnx = datastore.beginTransaction();
		try {
			Entity entity = new Entity(DEVICE_TYPE);
			entity.setProperty(DEVICE_REG_ID_PROPERTY, regId);
			datastore.put(entity);
			tnx.commit();
		}
		finally {
			if(tnx.isActive()){
				tnx.rollback();
			}
		}
	}
	
}
